#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "..//include//createvfs.h"
#include "..//include//n_ary.h"
#include "..//include//vfs_errorcodes.h"
#include "..//include//fileOperations.h"
#define pf printf


int movedirectory(char[],char[],char[]);

int movedirectory(char source_dir[],char des_dir[],char filename_g[])
{
int i,j,f1=0,f2=0,l1,l2,c,t,p[100],pos; int len1,len2;
char temp[100],temp2[100];
FILE *f;
struct header h;
f=fopen(filename_g,"rb+");//give exact path
fread(&h,sizeof(struct header),1,f);
fclose(f);
for(i=0;i<100;i++)
p[i]=-1;
i=0;

len1=strlen(source_dir);
len2=strlen(des_dir);
if(len1==0||len2==0)
{
   printf("movedir_FAILURE %s\n",ERR_VFS_MOVEDIR_00);
  return 0;
}
if(source_dir[len1-1]!='/') strcat(source_dir,"/");

len2=strlen(des_dir);
if(des_dir[len2-1]!='/') strcat(des_dir,"/");


if ((strcmp(source_dir,"") == 0) || (strcmp(des_dir,"") == 0)){
  printf("movedir_FAILURE %s\n",ERR_VFS_MOVEDIR_00);
  return 0;
}

if(strstr(des_dir,"."))
{
printf("movedir_FAILURE %s\n",ERR_VFS_MOVEDIR_07);
return(0);
}

if(strstr(source_dir,"."))
{
printf("movedir_FAILURE %s\n",ERR_VFS_MOVEDIR_04);
return(0);
}

if(strcmp(source_dir,des_dir)==0)
{
//pf("movedir_FAILURE\tSOURCE AND DESTINATION CAN'T BE SAME\n");
return 1;
}

for(i=1;i<TOTAL_DESC;i++)
{
if(strcmp(h.fd_arr[i].path,source_dir)==0)
{
f1=1;
break;
}
}

if(f1==0)
{
printf("movedir_FAILURE %s\n",ERR_VFS_MOVEDIR_01);
return(0);
}



for(i=1;i<TOTAL_DESC;i++)
{
if(strcmp(h.fd_arr[i].path,des_dir)==0)
{
f2=1;
break;
}
}

if(f2==0)
{
printf("movedir_FAILURE %s\n",ERR_VFS_MOVEDIR_02);
return(0);
}

l1=strlen(source_dir);
l2=strlen(des_dir);
t=0;

if(l1<l2)
{

for(c=0;c<l1;c++)
{
if(source_dir[c]==des_dir[c])
t++;
}

if(t==l1)
{
printf("movedir_FAILURE %s\n",ERR_VFS_MOVEDIR_06);
return(0);
}

}
t=1;



if(t==1&&strstr(source_dir,des_dir))
{
l1=strlen(source_dir);
l2=strlen(des_dir);
c=0;
j=0;
for(t=0;des_dir[t]!='\0';t++)
{
if(source_dir[t]==des_dir[t])
c++;
}

if(c==l2)
{
//printf("\n l2 position = %c",source_dir[l2]);
for(t=l2;source_dir[t]!='\0';t++)
if(source_dir[t]=='/')
j++;
}

if(j==1)
{
pf("movedir_FAILURE %s\n",ERR_VFS_MOVEDIR_05);
return(0);

}


}




strcpy(temp,"\0");
strcpy(temp2,"\0");
t=0;
i=1;
j=0;

while(i<TOTAL_DESC)
{
l1=strlen(source_dir);
l2=strlen(h.fd_arr[i].path);
t=0;
if(strstr(h.fd_arr[i].path,source_dir)&&l2>=l1)
{
for(c=0;source_dir[c]!='\0';c++)
{
if(source_dir[c]==h.fd_arr[i].path[c])
t++;
}
if(t==l1)
{
p[j]=i;
j++;
}
}
i++;
}

for(i=0;p[i]!=-1;i++)
{
c=0;
t=0;
l1=strlen(source_dir)-1;//m
l2=strlen(h.fd_arr[p[i]].path)-1;//m
/*while(h.fd_arr[p[i]].path[c]!='\0')
{
if(h.fd_arr[p[i]].path[c]!=des_dir[c])
{
pos=c;
break;
}
c++;
}*/
for(c=l1-1;h.fd_arr[p[i]].path[c]!='\0';c++)
{
temp[t]=h.fd_arr[p[i]].path[c];
t++;
}
temp[t]='\0';
strcpy(temp2,des_dir);
strcat(temp2,temp);
strcpy(h.fd_arr[p[i]].path,temp2);
}

f=fopen(filename_g,"rb+");
rewind(f);
fwrite(&h,sizeof(struct header),1,f);
fclose(f);


//printf("Before\n");
//RecDisplay(root);


Tree src_prev;
Tree src_cur;
Tree dest_prev;
Tree dest_cur;
Tree tempNode;
int s;
int d;

src_prev = reqd_node_prev(source_dir); 
dest_prev = reqd_node_prev(des_dir); 
src_cur = reqd_node_cur(source_dir); 
dest_cur = reqd_node_cur(des_dir); 
s = reqd_node_isSib(source_dir); 
d = reqd_node_isSib(des_dir); 




if(s){

	if(dest_cur->left == NULL)
		dest_cur->left = src_cur;
	else{
		tempNode = dest_cur->left;
		while(tempNode->right!=NULL){
			tempNode = tempNode->right;		
		}
		
		tempNode->right = src_cur;	
	}
	src_prev->left = src_cur->right;
	src_cur->right=NULL;
}

else{
	if(dest_cur->left == NULL)
		dest_cur->left = src_cur;
	else{
		tempNode = dest_cur->left;
		while(tempNode->right!=NULL){
			tempNode = tempNode->right;		
		}
		
		tempNode->right = src_cur;	
	}
	src_prev->right = src_cur->right;
	src_cur->right=NULL;
}




//printf("After\n");
//RecDisplay(root);

return 1;

}
